// const res = require("express/lib/response");
var count = 8;
var page = 1;
var yemashu
var del_id
var edit_id
var chakan_id
var tableData
var total

// 获取数据
// function showData() {
//     $.ajax({
//         url: '/get/tran',
//         data: {
//             sql1: sqlkind + `
// WHERE ${kindnum} LIMIT ${(page - 1) * count},${count}`,
//             sql2: sqlkind + ` WHERE ${kindnum}`
//         },
//         success: (res) => {
//             if (res.error) {
//                 console.log(error);
//             } else {
//                 // console.log(res);
//                 tableData = res.data
//                 total = res.total.length
//                 // console.log(tableData.length)
//                 // console.log(total)
//                 showTable();
//             }
//         }
//     })
// }
// 渲染表格
function showTable() {
    $('tbody').html('')
    for (let i = 0; i < tableData.length; i++) {
        $('tbody').append(`
                <tr data-id=${tableData[i].t_id}>
                    <td>${tableData[i].t_id}</td>
                    <td>${tableData[i].c2_name}</td>
                    <td>${tableData[i].t_methed}</td>
                    <td>${tableData[i].p_name}</td>
                    <td>${tableData[i].t_time}</td>
                    <td>
                        <a href="javascript:;" onclick="chakan(this)">查看</a>
                        <a href="javascript:;" class="del"  onclick="del(this)">删除</a>
                        <a href="javascript:;" class="edit" onclick="edit(this)">修改</a>
                    </td>
                </tr>`)
    }
    //渲染页码
    $('#yema').html('');
    if (Math.ceil(total / count) != 1) {
        $('#yema').append(
            ` <div onclick="shang()"> <p><</p> </div>`
        )
        for (let i = 1; i <= Math.ceil(total / count); i++) {
            if (page == i) {
                $('#yema').append(`
                        <div class="yemaxuan yema">${i}</div>
                        `)
            } else {
                $('#yema').append(`
                        <div class="yema">${i}</div>
                        `)
            }
        }
        $('#yema').append(
            ` <div onclick="xia()"> <p>></p> </div>`
        )
    }
    let yema = document.querySelectorAll('#yema .yema')
    // console.log(yema.length)
    yemashu = yema.length
}
//改变每个页面的条数
function gaiyema() {
    count = parseInt($('#gaiyema').val())
    page = 1
    sel()
}
//上一页
function shang() {
    // console.log(123)
    if (page != 1) {
        page--
        sel()
    }
}
// 下一页
function xia() {
    if (page < yemashu) {
        page++
        sel()
        // console.log(tableData);
    }
}
//跳转页面
function tiao() {
    // console.log($('#tiao').val())
    if (yemashu >= $('#tiao').val() && 0 < $('#tiao').val()) {
        page = $('#tiao').val()
        sel()
    }
}

// 添加正则
function yanzhengid() {
    let zhengze = /^ys\d{4}$/;
    let id = $('#t_id').val();
    let result = zhengze.test(id.trim());
    if (result && id != "") {
        // $('#t_id').next().html('')
        $('#t_id')[0].placeholder = ''
        $('#t_id')[0].style.border = '1px solid black'
    } else {
        $('#t_id')[0].placeholder = '格式有误！'
        $('#t_id')[0].style.border = '1px solid red'
    }
}
$('#add button').click(function () {
    $('#myModal').modal('toggle')
})
// 添加
$('.querentianjia').click(function () {
    $.ajax({
        url: '/add/tran_ve',
        method: 'post',
        data: {
            sql1: `SELECT * FROM purchase WHERE p_item=${$('#t_shopid').val()}`,
            sql2: `SELECT * FROM transport WHERE t_shopid=${$('#t_shopid').val()}`,
            sql3: `SELECT * FROM transport WHERE t_id='${$('#t_id').val()}'`,
            sql4: `INSERT INTO transport VALUES('${$('#t_id').val()}','${$('#t_methed').val()}','${$('#t_person').val()}'
            ,'${$('#t_shopid').val()}','${$('#t_time').val()}','${$('#t_state').val()}','${$('#t_warehouse').val()}') `,
        },
        success: (res) => {
            if (res.error) {
                console.log(res.error);
            } else {
                alert(res)
            }
        }
    })
})
// 页面跳转
$('#yema').on('click', '.yema', function () {
    page = parseInt($(this).text())
    // console.log(123)
    sel()
})
//修改
let pname = document.getElementById('pname')
let methed = document.getElementById('methed')
let box = document.getElementById('box')
let editdiv = document.getElementById('edit')
let ysid = document.getElementById('ysid')
let pid
function edit(obj) {
    // let prc = document.getElementById('prc')
    // console.log(prc)
    let ming = document.getElementById('ming')
    let kind = document.getElementById('kind')
    let pro = document.getElementById('pro')
    let pnum = document.getElementById('pnum')
    let pnweightum = document.getElementById('weight')
    edit_id = $(obj).parents('tr').attr('data-id');
    // console.log(edit_id);
    box.style.display = 'none'
    editdiv.style.display = 'block'
    $.ajax({
        url: '/cha/tran_ve',
        methed: 'get',
        data: {
            sql: sqlkind + `
            WHERE t_id='${edit_id}'`
        },
        success: (res) => {
            if (res.error) {
                console.log(error);
            } else {
                console.log(res.data)
                ysid.value = res.data[0].t_id
                ming.value = res.data[0].c2_name
                kind.value = res.data[0].v_kind
                methed.value = res.data[0].t_methed
                pname.value = res.data[0].p_name
                pid = res.data[0].p_id
                // car.value = res.data[0].p_carid
                pro.value = res.data[0].p_pro
                pnum.value = res.data[0].p_num
                pnweightum.value = res.data[0].p_weight
                if (shangpinzhonglei == 2) {
                    $('#shangpinzhonglei')[0].style.display = 'none'
                }
            }
        }
    })
}
function editPer(obj) {
    pid = parseInt($(obj).val())
    pname.value = $(obj).children().eq(pid - 1).text()
    // console.log(pid);
    console.log($(obj).children().eq(pid - 1).text());
}
function editMethed(obj) {
    methed.value = $(obj).val()
}
function editReturn() {
    box.style.display = 'block'
    editdiv.style.display = 'none'
    sel()
}
function editqueding() {
    $.ajax({
        url: '/update/tran',
        methed: 'get',
        data: {
            sql: `UPDATE transport SET t_person=${pid},t_methed="${methed.value}" WHERE t_id="${ysid.value}"`,
        },
        success: (res) => {
            if (res.error) {
                console.log(res.error)
            } else {
                alert(res)
            }
        }
    })
    // console.log(pname.value, methed.value, pid, ysid.value)
}
//查看
function chakan(obj) {
    chakan_id = $(obj).parents('tr').attr('data-id');
    // console.log(chakan_id);
    $('#myModal2').modal('toggle')
    let cha_prc = document.getElementById('cha_prc')
    let cha_ysid = document.getElementById('cha_ysid')
    let cha_shopname = document.getElementById('cha_shopname')
    let cha_shopid = document.getElementById('cha_shopid')
    let cha_methed = document.getElementById('cha_methed')
    let cha_person = document.getElementById('cha_person')
    let cha_time = document.getElementById('cha_time')
    let cha_state = document.getElementById('cha_state')
    let cha_house = document.getElementById('cha_house')
    $.ajax({
        url: '/cha/tran_ve',
        methed: 'get',
        data: {
            // id: chakan_id
            sql: sqlkind + `
            WHERE t_id='${chakan_id}'`
        },
        success: (res) => {
            if (res.error) {
                console.log(error);
            } else {
                console.log(res.data[0]);
                cha_prc.src = res.data[0].g_pic
                cha_ysid.value = res.data[0].t_id
                cha_shopname.value = res.data[0].c2_name
                // kind.value = res.data[0].v_kind
                cha_methed.value = res.data[0].t_methed
                cha_person.value = res.data[0].p_name
                cha_state.value = res.data[0].t_state
                cha_house.value = res.data[0].house_name + "仓      " + res.data[0].house_address
                // car.value = res.data[0].p_carid
                // pro.value = res.data[0].p_pro
                cha_shopid.value = res.data[0].p_num
                cha_time.value = res.data[0].t_time
            }
        }
    })
}
// 删除
function del(obj) {
    del_id = $(obj).parents('tr').attr('data-id');
    console.log(del_id);
    $('#exampleModal2').modal('toggle')
}
//删除
function quedingshanchu() {
    $.ajax({
        url: '/del/tran_ve',
        method: 'post',
        data: {
            // id: del_id
            sql: `delete from transport where t_id="${del_id}"`
        },
        success: (res) => {
            console.log(res);
            if (res.error) {
                alert(res)
            } else {
                alert(res);
                showData();
            }
        }
    })
}
// 查询
function sel() {
    // console.log(123);
    // $('#sel_t_per').val()
    // console.log($('#sel_t_per').val());
    if ($('#sel_t_per').val() == '') {
        if ($('#sel_t_met').val() == '全部') {
            // console.log(123);
            $.ajax({
                url: '/get/tran',
                data: {
                    sql1: sqlkind + `
                WHERE ${kindnum} LIMIT ${(page - 1) * count},${count}`,
                    sql2: sqlkind + `
                    WHERE ${kindnum}`
                },
                success: (res) => {
                    if (res.error) {
                        console.log(res.error)
                    } else {
                        tableData = res.data
                        // console.log(res);
                        total = res.total.length
                        showTable()
                    }
                }
            })
        }
        else {
            $.ajax({
                url: '/get/tran',
                data: {
                    sql1: sqlkind + `
            WHERE ${kindnum} AND t_methed='${$('#sel_t_met').val()}' LIMIT ${(page - 1) * count},${count}`,
                    sql2: sqlkind + `
                WHERE ${kindnum} AND t_methed='${$('#sel_t_met').val()}'`
                },
                success: (res) => {
                    if (res.error) {
                        console.log(res.error)
                    } else {
                        tableData = res.data
                        total = res.total.length
                        showTable()
                    }
                }
            })
        }
    }
    if ($('#sel_t_per').val() != '') {
        if ($('#sel_t_met').val() == '全部') {
            console.log(123);
            $.ajax({
                url: '/get/tran',
                data: {
                    sql1: sqlkind + `
                    WHERE ${kindnum} AND p_name='${$('#sel_t_per').val()}' LIMIT ${(page - 1) * count},${count}`,
                    sql2: sqlkind + `
                    WHERE ${kindnum} AND p_name='${$('#sel_t_per').val()}'`
                },
                success: (res) => {
                    if (res.error) {
                        console.log(res.error)
                    } else {
                        tableData = res.data
                        // console.log(tableData)
                        total = res.total.length
                        showTable()
                    }
                }
            })
        }
        else {
            $.ajax({
                url: '/get/tran',
                data: {
                    sql1: sqlkind + `
            WHERE ${kindnum} AND p_name='${$('#sel_t_per').val()}' AND t_methed='${$('#sel_t_met').val()}' LIMIT ${(page - 1) * count},${count}`,
                    sql2: sqlkind + `
                WHERE ${kindnum} AND p_name='${$('#sel_t_per').val()}' AND t_methed='${$('#sel_t_met').val()}'`
                },
                success: (res) => {
                    if (res.error) {
                        console.log(res.error)
                    } else {
                        tableData = res.data
                        total = res.total.length
                        showTable()
                    }
                }
            })
        }
    }
    // if ($('#sel_t_met').val() == '全部' && $('#sel_t_per').val() != '') {
    //     console.log(123);
    //     $.ajax({
    //         url: '/get/tran',
    //         data: {
    //             sql1: `SELECT * FROM transport
    //         LEFT JOIN tran_person ON transport.t_person=tran_person.p_id
    //         LEFT JOIN purchase ON transport.t_shopid=purchase.id
    //         LEFT JOIN goods ON purchase.p_item=goods.g_id
    //         LEFT JOIN class2 ON class2.c2_id=goods.g_class
    //         LEFT JOIN class1 ON class1.c1_id=class2.class1_id
    //         WHERE c1_id=1 LIMIT ${(page - 1) * count},${count}`,
    //             sql2: `SELECT COUNT(*) total FROM transport
    //             LEFT JOIN tran_person ON transport.t_person=tran_person.p_id
    //             LEFT JOIN purchase ON transport.t_shopid=purchase.id
    //             LEFT JOIN goods ON purchase.p_item=goods.g_id
    //             LEFT JOIN class2 ON class2.c2_id=goods.g_class
    //             LEFT JOIN class1 ON class1.c1_id=class2.class1_id
    //             WHERE c1_id=1`
    //         },
    //         success: (res) => {
    //             if (res.error) {
    //                 console.log(res.error)
    //             } else {
    //                 tableData = res.data
    //                 total = res.total
    //                 showTable()
    //             }
    //         }
    //     })
    // }
}
//重置
function reset() {
    console.log($('#sel_t_per').val());
    $('#sel_t_per').val("")
    $('#sel_t_met').val('全部')
    sel()
}
